var express = require('express');
const logoff = express.Router();
var bodyParser = require('body-parser');
const path = require("path");
const Database = require("better-sqlite3");
//获取数据库中管理员账号密码等信息
const mydb = new Database(path.resolve(__dirname, "../database/infor.db"), { verbose: console.log });

// 创建 application/x-www-form-urlencoded 编码解析
logoff.use(bodyParser.urlencoded({ extended: false }))
logoff.use(express.json());
logoff.use(bodyParser.json());

logoff.post('/booklogoff', function (req, res) {
    // 获取前端数据
    var response = {
        "code": req.body.code,
    };
    //指定数据库文件路径
    const bookcode = mydb.prepare('SELECT * FROM book WHERE code= ? ');//一个查询语句对象
    var a = bookcode.get(response.code);//执行查询
    if (a) {
        // 如果查询到了
        const updata_book = mydb.prepare('UPDATE book SET logoff =@logoff WHERE code=@code');
        updata_book.run({logoff: "yes", code: response.code });
        res.sendFile(path.resolve(__dirname, "../public/html/logoff_success.html"));

    }
    else {
        // 如果没有查询到，说明这本书不存在
        console.log("该书不存在");
        res.sendFile(path.resolve(__dirname, "../public/html/logoff_error.html"));
    }
});

module.exports = logoff;